/****************************************************************************************************
This .do file does the following:

1. Takes worker-teammate-choice (blame, free riding, relief time) level data and merges 
with baseline data
2. Cleans relevant variables
3. Saves for analysis 
****************************************************************************************************
****************************************************************************************************/

*-----------------------------------
 * Setup Environment
 *-----------------------------------
set more off
clear all
set segmentsize 3g  /* Setting memory */



* Import Data
use "$Data/Original/worker_teammate_interactions.dta", clear

* Baseline IDs
destring bid, gen(_id)

* Egg and Maida are common sections that serve all lines
replace Section = Line if inlist(Line, "Egg", "Maida")

* Merge Baseline Data
merge m:1 _id using "$Data/Final/Baseline_Cleaned.dta", keep(3) nogen

* Encode variables and create grouping identifier
encode Line, gen(line)
encode Section, gen(section)
egen line_sec_team = group(Team line section)

* Gen dummy variable if worker chose teammate for this question
destring C_R, gen(chosen)

* Identify Hindu and Muslim targets in Choices variable
gen rel_hindu = strpos(Choices, "(H)") > 0 /* Teammate being asked about is Hindu */
gen rel_muslim = strpos(Choices, "(M)") > 0 /* Teammate being asked about is Muslim */
keep if (rel_hindu == 1 | rel_muslim == 1)

* Generate Fixed Effects using line and section
xi i.line*i.section, prefix(_ls)
egen line_section = group(line section)


* Drop operators as these questions do not apply to them
drop if skill == 1  


*-------------------------------*
* Asssign Treatment Status -----*
*-------------------------------*
/*

This part is commented out since it had identifying info because production lines are named by product brands. Line names have been replace by numbers here. 

g mixed = 0
g dep = 0

        * Line 3
        replace dep = 1 if inlist(Section, "1St Line", "2Nd Line", "Cfc", "Depanning", "Packing") & Line == "Line 3"
        replace mixed = 1 if inlist(Section, "1St Line", "2Nd Line", "Cfc", "Depanning", "Packing") & inlist(Team, "X") & Line == "Line 3"
		replace mixed = 1 if !inlist(Section, "1St Line", "2Nd Line", "Cfc", "Depanning", "Packing")  & inlist(Team, "Y", "Z") & Line == "Line 3"

        * Line 1
		replace dep = 1 if inlist(Section, "Cfc", "Deposit", "Packing") & Line == "Line 1"
        replace mixed = 1 if inlist(Section, "Cfc", "Deposit", "Packing")  & inlist(Team, "X", "Y") & Line == "Line 1"
        replace mixed = 1 if !inlist(Section, "Cfc", "Deposit", "Packing") & inlist(Team, "Z") & Line == "Line 1"
        
		* Line 2
        replace dep = 1 if inlist(Section, "Cfc", "Deposit", "Packing", "Inject") & Line == "Line 2"
        replace mixed = 1 if inlist(Section, "Cfc", "Deposit", "Packing", "Inject")  & inlist(Team,  "Y") & Line == "Line 2"
        replace mixed = 1 if !inlist(Section, "Cfc", "Deposit", "Packing", "Inject") &  inlist(Team,  "X", "Z") & Line == "Line 2"
    
	    * Line 6
		replace dep = 1 if inlist(Section, "Box Filling", "Cfc") & Line == "Line 6"
        replace mixed = 1 if !inlist(Section, "Box Filling", "Cfc") & Line == "Line 6"
		
		* Line 5
    	replace dep = 1 if inlist(Section, "Box Filling", "Box Machine", "Cfc") & Line == "Line 5"
        replace mixed = 1 if inlist(Section, "Box Filling", "Box Machine", "Cfc") & inlist(Team, "X", "Z") & Line == "Line 5"
        replace mixed = 1 if !inlist(Section, "Box Filling", "Cfc") & inlist(Team, "Y") & Line == "Line 5"
    
	    * Line 4
        replace dep = 1 if inlist(Section, "Packing", "Cfc")  & Line == "Line 4"
        replace mixed = 1 if inlist(Section, "Packing", "Cfc") &  inlist(Team, "Y") & Line == "Line 4"
        replace mixed = 1 if !inlist(Section, "Packing", "Cfc")  & inlist(Team, "X") & Line == "Line 4"
 
        * Maida
        replace dep = 1 if Line == "Maida"
        replace mixed = 1 if inlist(Team, "Y", "Z") & Line == "Maida"
 
        * Egg
        replace dep = 0 if Line == "Egg"
        replace mixed = 1 if inlist(Team, "X", "Y") & Line == "Egg"
 
*/

* Label variables
la var Choices "[pre-loaded] string variable denoting choice type (blame, free rider etc.), teammate name, and religion (only names were read out to respondents)"
la var chosen "=1 if worker has chosen teammate for this choice type, 0 otherwise"
la var rel_hindu "=1 if person being asked about is Hindu"
la var rel_muslim "=1 if person being asked about is Muslim"
la var Choice_Relief "=1 if question is regarding RELIEF TIME"
la var Choice_Overtime "=1 if question is regarding OVERTIME"
la var Free_Riding "=1 if question is regarding FREE RIDING"
la var Blame "=1 if question is regarding BLAME"
la var relief_nonteam "=1 if question is regarding RELIEF TIME(for non-teammate)"
la var overtime_nonteam "=1 if question is regarding OVERTIME (for non-teammate)"


** HD vs LD dummies for sections **
gen LD = (dep == 0)
gen HD = (dep == 1) 

* Interactions of mixed with treatment variables
gen mixedXLD = mixed * LD
gen mixedXHD = mixed * HD

* Save final dataset
save "$Data/Final/worker_choices.dta", replace
